﻿using MCPlatform.MotionLib.Base.AssitantClass;
using MCPlatform.MotionLib.Base.Environment;
using Panuon.WPF;
using Panuon.WPF.UI;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using System.Windows.Input;

namespace MCPlatform.MotionLib.Base.Models
{
    public class MotionParaModel: NotifyPropertyChangedBase,INameProvider
    {
        #region Properties

        [Newtonsoft.Json.JsonIgnore]
        public IMotion? MotionEntity
        {
            get => _MotionEntity;
            set
            {
                Set(ref _MotionEntity, value);

            }
        }
        [Newtonsoft.Json.JsonIgnore]
        private IMotion? _MotionEntity;


        /// <summary>
        /// 坐标系号名称
        /// </summary>

        public string ControlName
        {
            get => _ControlName;
            set
            {
                Set(ref _ControlName, value);

            }
        }

        private string _ControlName = string.Empty;



        /// <summary>
        /// 脉冲当量
        /// </summary>
        public double Equiv
        {
            get => _Equiv;
            set => Set(ref _Equiv, value);
        }
        private double _Equiv;


        /// <summary>
        /// 最小速度
        /// </summary>
        public double MinVelocity
        {
            get => _MinVelocity;
            set => Set(ref _MinVelocity, value);
        }
        private double _MinVelocity=1;

        /// <summary>
        /// 最大速度
        /// </summary>
        public double MaxVelocity
        {
            get => _MaxVelocity;
            set => Set(ref _MaxVelocity, value);
        }
        private double _MaxVelocity=200;

        /// <summary>
        /// 加速度
        /// </summary>
        public double Acc
        {
            get => _Acc;
            set => Set(ref _Acc, value);
        }
        private double _Acc = 0.05;

        /// <summary>
        /// 减速度
        /// </summary>
        public double Dec
        {
            get => _Dec;
            set => Set(ref _Dec, value);
        }
        private double _Dec = 0.05;

        /// <summary>
        /// 停止速度
        /// </summary>
        public double StopVelocity
        {
            get => _StopVelocity;
            set => Set(ref _StopVelocity, value);
        }
        private double _StopVelocity;

        /// <summary>
        /// S曲线模式
        /// </summary>
        public ushort SMode
        {
            get => _SMode;
            set => Set(ref _SMode, value);
        }
        private ushort _SMode = 0;

        /// <summary>
        /// S曲线时间
        /// </summary>
        public double SPara
        {
            get => _SPara;
            set => Set(ref _SPara, value);
        }
        private double _SPara ;


        public bool IsEnableSoftLimit
        {
            get => _isEnableSoftLimit;
            set => Set(ref _isEnableSoftLimit, value);
        }
        private bool _isEnableSoftLimit = true;

        public double SPLimit
        {
            get => _SPLimit;
            set => Set(ref _SPLimit, value);
        }
        private double _SPLimit = 999999.99;

        public double SNLimit
        {
            get => _SNLimit;
            set => Set(ref _SNLimit, value);
        }
        private double _SNLimit = -999999.99;

        public double Backlash
        {
            get => _backlash;
            set => Set(ref _backlash, value);
        }
        private double _backlash;



        // 回原参数


        /// <summary>
        /// 回原低速
        /// </summary>
        public double HomingLowerVelocity
        {
            get => _HomingLowerVelocity;
            set => Set(ref _HomingLowerVelocity, value);
        }
        private double _HomingLowerVelocity;

        /// <summary>
        /// 回原高速
        /// </summary>
        public double HomingHeightVelocity
        {
            get => _HomingHeightVelocity;
            set => Set(ref _HomingHeightVelocity, value);
        }
        private double _HomingHeightVelocity;

        /// <summary>
        /// 回原加速度
        /// </summary>
        public double HomingAcc
        {
            get => _HomingAcc;
            set => Set(ref _HomingAcc, value);
        }
        private double _HomingAcc=0.05;


        /// <summary>
        /// 回原减速度
        /// </summary>
        public double HomingDec
        {
            get => _HomingDec;
            set => Set(ref _HomingDec, value);
        }
        private double _HomingDec = 0.05;


        /// <summary>
        /// 回原模式
        /// </summary>
        public ushort HomingMode
        {
            get => _HomingMode;
            set => Set(ref _HomingMode, value);
        }
        private ushort _HomingMode = 0;

        /// <summary>
        /// 回原偏移值
        /// </summary>
        public double HomingOffSet
        {
            get => _HomingOffSet;
            set => Set(ref _HomingOffSet, value);
        }
        private double _HomingOffSet;

        [Newtonsoft.Json.JsonIgnore]
        public ICommand? ApplyCommand
        {
            get => _ApplyCommand;
            set => Set(ref _ApplyCommand, value);
        }
        [Newtonsoft.Json.JsonIgnore]
        private ICommand? _ApplyCommand;

        [Newtonsoft.Json.JsonIgnore]
        public ICommand? ApplyAndSaveCommand
        {
            get => _ApplyAndSaveCommand;
            set => Set(ref _ApplyAndSaveCommand, value);
        }
        [Newtonsoft.Json.JsonIgnore]
        private ICommand? _ApplyAndSaveCommand;

        #endregion

     
        public MotionParaModel()
        {
            ApplyCommand = new Command(Apply);

            ApplyAndSaveCommand = new Command(ApplyAndSave);

           
        }

        /// <summary>
        /// 应用
        /// </summary>
        /// <param name="O"></param>
        public void Apply(Object O) 
        {
            if (MotionEntity is null)
            {
                return;
            }
            Common.AutoMapProperties(this, MotionEntity);
            Common.AutoMapProperties(this,MotionEntity.AxisConfig);
            MotionEntity.SetProfile();
            Toast.Show("应用成功!",targetWindow:ToastWindow.MainWindow);
        }

       /// <summary>
       /// 应用并保存
       /// </summary>
       /// <param name="O"></param>
        public async void ApplyAndSave(Object O)
        {
            if (MotionEntity is null)
            {
                return;
            }

            await Task.Run(() =>
            {
                Common.AutoMapProperties(this, MotionEntity);
                Common.AutoMapProperties(this, MotionEntity.AxisConfig);

                MotionEntity.SetProfile();
             
               
            });


            SaveConfigData.SaveConfig<MotionParaModel>(Paths.MotionConfigPaths, this);

            //String path = Paths.MotionConfigPaths;

            //var MotionParaInfolList = ConvertJson.ReadFileToList<MotionParaInfo>(path);

            ////创建轴配置保存类
            //MotionParaInfo motionParaInfo = new MotionParaInfo();
            ////从当前类映射信息至配置类
            //Common.AutoMapPropertiesNotInterface(this, motionParaInfo);

            //if (MotionParaInfolList != null)
            //{
            //    foreach (var item in MotionParaInfolList)
            //    {
            //        if (item.AxisName is null)
            //        {
            //            return;
            //        }
            //        if (item.AxisName.Equals(this.AxisName))
            //        {
            //            MotionParaInfolList[MotionParaInfolList.IndexOf(item)] = motionParaInfo;
            //            ConvertJson.WriteJsonToFile(path, MotionParaInfolList);
            //            Toast.Show("应用成功，已保存", targetWindow: ToastWindow.MainWindow);
            //            return;

            //        }

            //    }
            //    MotionParaInfolList.Add(motionParaInfo);
            //    ConvertJson.WriteJsonToFile(path, MotionParaInfolList);
            //    Toast.Show("未找到此轴号配置，已创建并写入成功！", targetWindow: ToastWindow.MainWindow);
            //}
            //else
            //{

            //    MotionParaInfolList = new List<MotionParaInfo>
            //    {
            //        motionParaInfo,
            //    };
            //    ConvertJson.WriteJsonToFile(path, MotionParaInfolList);
            //    Toast.Show("未找到配置文件，已创建并写入成功！", targetWindow: ToastWindow.MainWindow);
            //}


        }

        

        public void MappingFromEntity()
        {
            if (MotionEntity is null)
            {
                return;
            }
            MotionEntity.AxisConfig.ControlName = MotionEntity.ControlName;
          //  Common.AutoMapPropertiesNotInterface(MotionEntity, this);
            Common.AutoMapPropertiesNotInterface(MotionEntity.AxisConfig,this);
        }
    }
}
